package com.huluobo.user.view

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.google.gson.Gson
import com.huluobo.lib_base.RoutePath
import com.huluobo.lib_base.UIState
import com.huluobo.user.R
import com.huluobo.user.UserIntent
import com.huluobo.user.UserViewModel
import com.huluobo.user.entity.ApproveEntity
import com.huluobo.user.entity.ApproveEntityItem
import kotlinx.coroutines.launch

/**
 *  Created by LC on 2025/3/18.
 */
@Composable
fun ApproveListPage(navHostController: NavHostController, vm: UserViewModel = hiltViewModel()) {
    val list = remember {
        SnapshotStateList<ApproveEntityItem>()
    }

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect {
                when (it) {
                    is UIState.OnError -> {
                        Log.e("TagA", "获取请假列表失败:${it.msg}")
                    }

                    UIState.OnLoading -> {}
                    is UIState.OnSuccess<*> -> {
                        list.clear()
                        list.addAll(it.data as ApproveEntity)
                    }
                }
            }
        }
        vm.sendIntent(UserIntent.GetApprove)
    })

    Column(Modifier.padding(10.dp)) {
        Row(
            Modifier
                .fillMaxWidth()
                .padding(10.dp), horizontalArrangement = Arrangement.SpaceBetween
        ) {
            Icon(imageVector = Icons.Default.ArrowBack, contentDescription = "", modifier = Modifier.clickable {
                navHostController.popBackStack()
            })
            Text(text = "请假列表")
            Text(
                text = "申请",
                Modifier
                    .background(colorResource(id = R.color.orange), RoundedCornerShape(5.dp))
                    .padding(10.dp, 3.dp)
                    .clickable {
                        navHostController.navigate(RoutePath.ADD_APPROVE)
                    }
            )
        }

        Spacer(modifier = Modifier.height(10.dp))
        Row(
            Modifier
                .fillMaxWidth()
                .background(colorResource(id = R.color.lightblue))
                .padding(10.dp),
            horizontalArrangement = Arrangement.SpaceBetween
        ) {
            Text(text = "类型")
            Text(text = "开始时间")
            Text(text = "结束时间")
            Text(text = "审批状态")
        }
        Spacer(modifier = Modifier.height(10.dp))
        LazyColumn(verticalArrangement = Arrangement.spacedBy(10.dp), content = {
            items(list) {
                ApproveItem(entityItem = it, navHostController)
            }
        })
    }
}

@Composable
fun ApproveItem(entityItem: ApproveEntityItem, navHostController: NavHostController) {
    Row(
        Modifier
            .fillMaxWidth()
            .background(colorResource(id = R.color.lightgray), RoundedCornerShape(10.dp))
            .padding(10.dp)
            .clickable {
                if (entityItem.status == "通过" || entityItem.status == "拒绝") {
                    val json = Gson().toJson(entityItem)
                    navHostController.navigate(RoutePath.APPROVE + "/$json")
                }
            },
        horizontalArrangement = Arrangement.SpaceBetween
    ) {
        Text(text = "${entityItem.type}")
        if (!entityItem.startTime.isNullOrEmpty()) {
            Text(text = "${entityItem.startTime.substring(0, 10)}")
        }
        if (!entityItem.endTime.isNullOrEmpty()) {
            Text(text = "${entityItem.endTime.substring(0, 10)}")
        }
        val color =
            if (entityItem.status == "通过") Color.Green else if (entityItem.status == "拒绝") Color.Red else Color.LightGray
        Text(
            text = entityItem.status,
            Modifier
                .background(color = color, RoundedCornerShape(5.dp))
                .padding(5.dp)
        )
    }
}